package com.tom.architect02.ui30.day23;

import android.content.Context;
import android.graphics.Color;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.RelativeLayout;

import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.DecelerateInterpolator;
/**
 * Added by Tom on 2024/06/20.
 */
public class LoadingView extends RelativeLayout {
    private Context mContext;
    private CircleView mLeftView,mMiddleView,mRightView;
    private int mTranslationDistance = 30;
    private final long ANIMATION_TIME = 350;

    public LoadingView(Context context) {
        this(context, null);
    }

    public LoadingView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public LoadingView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.mContext = context;
        initLayout();
    }

    private void initLayout() {
        // LayoutInflater，也就是Activity.onCreate. 此时布局还没有实例化。
        mTranslationDistance = dip2px(mTranslationDistance);
        setBackgroundColor(Color.TRANSPARENT); // 设置背景。
        // 添加三个View 但是要圆形
        mLeftView = getCircleView(mContext);
        mLeftView.exchangeColor(Color.BLUE);
        mMiddleView = getCircleView(mContext);
        mMiddleView.exchangeColor(Color.RED);
        mRightView = getCircleView(mContext);
        mRightView.exchangeColor(Color.GREEN);
        addView(mLeftView);
        addView(mRightView);
        addView(mMiddleView); // 中间的点，在最上面

        post(new Runnable() {
            @Override
            public void run() {
                // 优化：让布局实例化好之后再去开启动画，Resume之后执行。
                expendAnimation();
            }
        });
    }

    /**
     * 展开动画
     */
    private void expendAnimation() {
        // 左边跑. 跑到左边去
        ObjectAnimator leftTranslationAnimator = ObjectAnimator.ofFloat(mLeftView,
                "translationX",0, -mTranslationDistance);
        // 右边跑，跑到右边去
        ObjectAnimator rightTranslationAnimator = ObjectAnimator.ofFloat(mRightView,
                "translationX",0, mTranslationDistance);
        // 弹性效果  荡秋千一样 差值器   刚开始快越来越慢
        AnimatorSet set = new AnimatorSet();
        set.setDuration(ANIMATION_TIME);
        set.playTogether(leftTranslationAnimator,rightTranslationAnimator);
        set.setInterpolator(new DecelerateInterpolator()); // 减速插值器，越来越慢
        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                // 监听动画执行完成：往里面跑
                innerAnimation();
            }
        });
        set.start();
    }

    // 动画：往里面跑
    private void innerAnimation() {
        // 左边跑：往中间跑
        ObjectAnimator leftTranslationAnimator = ObjectAnimator.ofFloat(mLeftView,
                "translationX",-mTranslationDistance, 0);
        // 右边跑：往中间跑
        ObjectAnimator rightTranslationAnimator = ObjectAnimator.ofFloat(mRightView,
                "translationX",mTranslationDistance, 0);
        AnimatorSet set = new AnimatorSet();
        set.setInterpolator(new AccelerateInterpolator()); // 加速插值器，越来越快
        set.setDuration(ANIMATION_TIME);
        set.playTogether(leftTranslationAnimator,rightTranslationAnimator);
        set.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
                // 往里外面跑
                // 切换颜色顺序：左边的给中间 中间的给右边  右边的给左边
                int leftColor = mLeftView.getColor(); // 拿到颜色
                int rightColor = mRightView.getColor();
                int middleColor = mMiddleView.getColor();
                mMiddleView.exchangeColor(leftColor); // 切花颜色
                mRightView.exchangeColor(middleColor);
                mLeftView.exchangeColor(rightColor);
                expendAnimation();
            }
        });
        set.start();
    }

    // 新建圆形的circle对象。
    public CircleView getCircleView(Context context) {
        CircleView circleView = new CircleView(context);
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(dip2px(10), dip2px(10));
        params.addRule(CENTER_IN_PARENT); // 添加到 RelativeLayout 的中间
        circleView.setLayoutParams(params);
        return circleView;
    }
    private int dip2px(int dip) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,dip,getResources().getDisplayMetrics());
    }

    // TODO: 优化setVisibility，导致内存泄漏，Activity不可回收。
    // TODO: 优化AnimatorSet，避免每次都创建新的对象。



}
